<?php

/**
 * 获取分类列表(分页)
 */
if($uri == "/api/admin/categories"){
    $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
    $size = isset($_GET['size']) ? intval($_GET['size']) : 10;
    $search = isset($_GET['search']) ? trim($_GET['search']) : '';
    
    // 构建查询条件
    $where = "1=1";
    if($search !== '') {
        $search = addslashes($search);
        $where .= " AND name LIKE '%$search%'";
    }
    $where .= " ORDER BY sort_order ASC";

    $total = 0; // 声明总数变量
    // 获取分类列表
    $categories = $cnSql->read("template_categories", $page, $size, $where, $total);
    
    // 获取每个分类下的模板数量
    foreach($categories as &$category) {
        $template_count = $cnSql->total("templates", "category_id = " . $category['id']);
        $category['template_count'] = $template_count;
    }
    
    $outJson->data = $categories;
    $outJson->total = intval($total);
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}

/**
 * 获取所有分类(不分页,用于下拉选择)
 */
if($uri == "/api/admin/categories/all"){
    // 只获取启用状态的分类
    $categories = $cnSql->custom("SELECT id, name FROM template_categories WHERE is_active = 1 ORDER BY sort_order ASC");
    
    $outJson->data = $categories;
    $outJson->code = 200;
    echo json_encode($outJson);
    exit;
}

/**
 * 创建分类
 */
if($uri == "/api/admin/category/create"){
    $post = json_decode(file_get_contents('php://input'), true);
    
    // 验证必填字段
    if(empty($post['name'])) {
        $outJson->msg = "分类名称不能为空";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    }

    // 检查名称长度
    if(mb_strlen($post['name']) > 50) {
        $outJson->msg = "分类名称不能超过50个字符";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    }

    // 检查分类名称是否已存在
    $exists = $cnSql->read_only("template_categories", "name = '" . addslashes($post['name']) . "'");
    if($exists) {
        $outJson->msg = "分类名称已存在";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    }

    // 插入数据
    $data = array(
        'name' => $post['name'],
        'description' => isset($post['description']) ? $post['description'] : '',
        'sort_order' => isset($post['sort_order']) ? intval($post['sort_order']) : 0,
        'is_active' => isset($post['is_active']) ? intval($post['is_active']) : 1
    );

    $id = $cnSql->insert_json("template_categories", $data);
    if($id) {
        $outJson->msg = "创建成功";
        $outJson->code = 200;
    } else {
        $outJson->msg = "创建失败";
        $outJson->code = 500;
    }
    echo json_encode($outJson);
    exit;
}

/**
 * 更新分类
 */
if($uri == "/api/admin/category/update"){
    $post = json_decode(file_get_contents('php://input'), true);
    
    if(empty($post['id'])) {
        $outJson->msg = "分类ID不能为空";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    }

    // 检查分类是否存在
    $category = $cnSql->read_only("template_categories", "id = " . intval($post['id']));
    if(!$category) {
        $outJson->msg = "分类不存在";
        $outJson->code = 404;
        echo json_encode($outJson);
        exit;
    }

    // 如果修改了名称,检查是否与其他分类重名
    if(isset($post['name']) && $post['name'] !== $category['name']) {
        $exists = $cnSql->read_only("template_categories", "name = '" . addslashes($post['name']) . "' AND id != " . $category['id']);
        if($exists) {
            $outJson->msg = "分类名称已存在";
            $outJson->code = 400;
            echo json_encode($outJson);
            exit;
        }
    }

    // 更新数据
    $data = array();
    if(isset($post['name'])) $data['name'] = $post['name'];
    if(isset($post['description'])) $data['description'] = $post['description'];
    if(isset($post['sort_order'])) $data['sort_order'] = intval($post['sort_order']);
    if(isset($post['is_active'])) $data['is_active'] = intval($post['is_active']);

    $result = $cnSql->update_json("template_categories", $data, "id = " . intval($post['id']));
    if($result) {
        $outJson->msg = "更新成功";
        $outJson->code = 200;
    } else {
        $outJson->msg = "更新失败";
        $outJson->code = 500;
    }
    echo json_encode($outJson);
    exit;
}

/**
 * 删除分类
 */
if($uri == "/api/admin/category/delete"){
    $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
    
    if(!$id) {
        $outJson->msg = "分类ID不能为空";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    }

    // 检查分类下是否有模板
    $template_count = $cnSql->total("templates", "category_id = $id");
    if($template_count > 0) {
        $outJson->msg = "该分类下还有{$template_count}个模板,不能删除";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    }

    $result = $cnSql->delete("template_categories", "id = $id");
    if($result) {
        $outJson->msg = "删除成功";
        $outJson->code = 200;
    } else {
        $outJson->msg = "删除失败";
        $outJson->code = 500;
    }
    echo json_encode($outJson);
    exit;
}

/**
 * 更新分类状态
 */
if($uri == "/api/admin/category/status"){
    $post = json_decode(file_get_contents('php://input'), true);
    
    if(empty($post['id'])) {
        $outJson->msg = "分类ID不能为空";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    }

    $result = $cnSql->update_json("template_categories", 
        array('is_active' => intval($post['is_active'])), 
        "id = " . intval($post['id'])
    );

    if($result) {
        $outJson->msg = "状态更新成功";
        $outJson->code = 200;
    } else {
        $outJson->msg = "状态更新失败";
        $outJson->code = 500;
    }
    echo json_encode($outJson);
    exit;
}

/**
 * 更新分类排序
 */
if($uri == "/api/admin/category/sort"){
    $post = json_decode(file_get_contents('php://input'), true);
    
    if(empty($post['id'])) {
        $outJson->msg = "分类ID不能为空";
        $outJson->code = 400;
        echo json_encode($outJson);
        exit;
    }

    $result = $cnSql->update_json("template_categories", 
        array('sort_order' => intval($post['sort_order'])), 
        "id = " . intval($post['id'])
    );

    if($result) {
        $outJson->msg = "排序更新成功";
        $outJson->code = 200;
    } else {
        $outJson->msg = "排序更新失败";
        $outJson->code = 500;
    }
    echo json_encode($outJson);
    exit;
}

?>